package cn.com.captureNetPacket.dependThirdPartyImpl;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

import cn.com.captureNetPacket.CaptureNetPacketService;
import cn.com.captureNetPacket.dependThirdParty.DependThirdPartyService;
import cn.com.captureNetPacket.model.DeviceStatus;
import cn.com.captureNetPacket.model.SignallingInfo;
import cn.com.captureNetPacket.util.JsonPathUtil;
import cn.com.captureNetPacket.util.RedisConfig;
import cn.com.captureNetPacket.util.Tools;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
@ConditionalOnProperty(name = "cnp.dependThirdParty.enable", havingValue = "true",matchIfMissing = false)
public class DependThirdPartyRedis implements DependThirdPartyService{
	 @Value("${cnp.dependThirdParty.dependThirdPartyRedisPrefix}")
	 private   String dependThirdPartyRedisprefix;
		
	
	@Override
	public SignallingInfo improveAndPerfectSignallingInfo(SignallingInfo signallingInfo) {
		
		 DeviceStatus deviceStatus=null;
		 if(signallingInfo.getDeviceNumber()!=null)
			 deviceStatus=waitDeviceStatus(signallingInfo.getDeviceNumber());
		if(deviceStatus!=null) {
			//订阅到后应该可以拿到所有，可以返回
			setImprove2SignallingInfo(deviceStatus,signallingInfo);
			return signallingInfo;
		}
		
		 if(signallingInfo.getDeviceNumber()!=null  ) {
			 // 分机号获取callid
			 setImprove2SignallingInfo(signallingInfo.getDeviceNumber(),signallingInfo);
		 } 
		 
		return signallingInfo;
	}

	@Override
	public SignallingInfo getSignallingInfo(SignallingInfo signallingInfo) {
		 DeviceStatus deviceStatus=null;
		 if(signallingInfo.getDeviceNumber()!=null)
			 deviceStatus= waitDeviceStatus(signallingInfo.getDeviceNumber());
		 
		
		if(deviceStatus!=null) {
			//订阅到后应该可以拿到所有，可以返回
			setSignallingInfo(deviceStatus,signallingInfo);
			return signallingInfo;
		}
		
		 if(signallingInfo.getDeviceNumber()!=null  ) {
			 // 
			 setSignallingInfo(signallingInfo.getDeviceNumber(),signallingInfo);
		 } 
		 
		return signallingInfo;
	}

	private void setSignallingInfo(String key, SignallingInfo signallingInfo) {
		String agentId=null,callInfo = null;
		try {
			callInfo=RedisConfig.getValue(dependThirdPartyRedisprefix, key);
			 if(callInfo!=null)
			 { 
				 signallingInfo.setCallUUID(JsonPathUtil.getValue(callInfo, CaptureNetPacketService.getConfig().getDependThirdPartyRedisExpressionCallId()));
			 log.debug("setSignallingInfo: agentId: {} \r\n callInfo :{} \r\n signallingInfo:{}",agentId,callInfo,Tools.getjsonString(signallingInfo));
			 }
		}catch (Exception e) {
			log.error("setSignallingInfo: {} \r\n signallingInfo:{}",callInfo,Tools.getjsonString(signallingInfo),e);
		}
	}

	@Override
	public void initDependThirdPartyServiceTask() {
		log.info("开启了redis依赖");
		
	}
	public void setImprove2SignallingInfo(String key,SignallingInfo signallingInfo) {
		String agentId=null,callInfo = null;
		try {
			callInfo=RedisConfig.getValue(dependThirdPartyRedisprefix, key);
			 if(callInfo!=null)
			 { 
				 signallingInfo.setCallUUID(JsonPathUtil.getValue(callInfo, CaptureNetPacketService.getConfig().getDependThirdPartyRedisExpressionCallId()));
			 log.debug("setImprove2SignallingInfo: agentId: {} \r\n callInfo :{} \r\n signallingInfo:{}",agentId,callInfo,Tools.getjsonString(signallingInfo));
			 }
		}catch (Exception e) {
			log.error("setImprove2SignallingInfo: {} \r\n signallingInfo:{}",callInfo,Tools.getjsonString(signallingInfo),e);
		}
	}
	public void setImprove2SignallingInfo(DeviceStatus deviceStatus,SignallingInfo signallingInfo) {
		try {
			log.debug("setImprove2SignallingInfo---------------------》》》》》》》》》》》》: deviceStatus: {}  \r\n signallingInfo:{}",Tools.getjsonString(deviceStatus),Tools.getjsonString(signallingInfo));
			signallingInfo.setCallUUID(deviceStatus.getCallUUID());
			signallingInfo.setAgentID(deviceStatus.getAgentID());
			signallingInfo.setCalled(deviceStatus.getCalled());
			signallingInfo.setCaller(deviceStatus.getCaller());
			signallingInfo.setDirection(deviceStatus.getDirection());
			if(signallingInfo.getRingTime()==null&& deviceStatus.getRingTime()!=null) {
				signallingInfo.setRingTime(deviceStatus.getRingTime());
			}
			if(signallingInfo.getEstablishTime()==null&& deviceStatus.getEstablishTime()!=null) {
				signallingInfo.setEstablishTime(deviceStatus.getEstablishTime());
			}
			if(signallingInfo.getDirection()==null&& deviceStatus.getDirection()!=null) {
				signallingInfo.setDirection(deviceStatus.getDirection());
			}
			 
			log.debug("setImprove2SignallingInfo: deviceStatus: {}  \r\n signallingInfo:{}",Tools.getjsonString(deviceStatus),Tools.getjsonString(signallingInfo));
		}catch (Exception e) {
			log.error("setImprove2SignallingInfo: deviceStatus: {}  \r\n signallingInfo:{}",Tools.getjsonString(deviceStatus),Tools.getjsonString(signallingInfo),e);
		}
	}
	
	 
	public void setSignallingInfo(DeviceStatus deviceStatus,SignallingInfo signallingInfo) {
		try {
			signallingInfo.setCallUUID(deviceStatus.getCallUUID());
			signallingInfo.setAgentID(deviceStatus.getAgentID());
			signallingInfo.setFromUser(deviceStatus.getCaller());
			signallingInfo.setToUser(deviceStatus.getCalled());
			signallingInfo.setCaller(deviceStatus.getCaller());
			signallingInfo.setCalled(deviceStatus.getCalled());
			signallingInfo.setDirection(deviceStatus.getDirection());
			if(signallingInfo.getBeginTime()==null&& deviceStatus.getBeginTime()!=null) {
				signallingInfo.setBeginTime(deviceStatus.getBeginTime());
			}
			if(signallingInfo.getRingTime()==null&& deviceStatus.getRingTime()!=null) {
				signallingInfo.setRingTime(deviceStatus.getRingTime());
			}
			if(signallingInfo.getEstablishTime()==null&& deviceStatus.getEstablishTime()!=null) {
				signallingInfo.setEstablishTime(deviceStatus.getEstablishTime());
			}
			
			log.debug("setSignallingInfo: deviceStatus: {}  \r\n signallingInfo:{}",Tools.getjsonString(deviceStatus),Tools.getjsonString(signallingInfo));
		}catch (Exception e) {
			log.error("setSignallingInfo: deviceStatus: {}  \r\n signallingInfo:{}",Tools.getjsonString(deviceStatus),Tools.getjsonString(signallingInfo),e);
		}
	}
	
	
	
	
	public void getCallInfo(String callInfoString,SignallingInfo signallingInfo) {
		try {
			
		Map<String,Object> callInfoObject=JsonPathUtil.getValue(callInfoString, CaptureNetPacketService.getConfig().getDependThirdPartyRedisExpressionCallId(),Map.class);
		signallingInfo.setCallUUID(callInfoObject.get("callid").toString());
		// 0呼出，1 呼入
// 		int direction =Integer.parseInt(callInfoObject.get("direction").toString());
		signallingInfo.setCaller(callInfoObject.get("caller").toString());
		signallingInfo.setCalled(callInfoObject.get("called").toString());
		signallingInfo.setAgentID(callInfoObject.get("agentid").toString());
		
		}catch (Exception e) {
			log.error("getCallInfo: {} \r\n signallingInfo:{}",callInfoString,Tools.getjsonString(signallingInfo),e);
		}
		log.debug("getCallInfo: {} \r\n signallingInfo:{}",callInfoString,Tools.getjsonString(signallingInfo));
	}

	@Override
	public DeviceStatus waitDeviceStatus(String deviceNumber) {
		 DeviceStatus deviceStatus=null;
		 Object obj=new Object();
		 long timeOut=CaptureNetPacketService.getWaitDeviceStatusTimeOut();
		 
		 log.debug("waitDeviceStatus deviceNumber : {}  开始获取设备状态. timeOut:{}",deviceNumber,timeOut);
		 while(deviceStatus==null && timeOut>0) {
			 try {
				deviceStatus= CaptureNetPacketService.getDeviceStatus().get(deviceNumber);
				if(deviceStatus!=null) {
					log.info("waitDeviceStatus  已经获取到设备状态   : {}  剩余获取时间 {} 秒",deviceStatus,timeOut);
					return deviceStatus;
				}
				synchronized (obj) {
					obj.wait(timeOut);
				}
				timeOut=timeOut-100;
				log.warn("waitDeviceStatus deviceNumber : {}  剩余获取时间 {} 秒",deviceNumber,timeOut);
			} catch (Exception e) {
			 log.error("waitDeviceStatus deviceNumber :{}  ,timeOut: {}",deviceNumber,timeOut,e);
			}
		 }
		 log.warn("waitDeviceStatus deviceNumber : {}  没有获取 到设备状态信息",deviceNumber);
		return deviceStatus;
	}

 
 
	
	

}
